﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>

<script type="text/javascript">
    $(function(){
         intityTxlTree();
    })
    
    function intityTxlTree() {
        
        $.ajax({
            url: "/Home/getTxlTree",
            type: "POST",
            success: function(txlNodes) {
                setting.callback.onClick = OnTxltreeClick;
                setting.callback.onRename = OnTxltreeOnRename;
                $.fn.zTree.init($("#treeDemo_txlmrg"), setting, eval(txlNodes));
            }
        });
    }
    function OnTxltreeClick(event, treeId, treeNode) {
        //alert(treeNode.tId + ", " + treeNode.name + " , " + treeNode.id + " , " + treeNode.tag);
         //循环获取根节点
         var rootNode=treeNode.getParentNode();
         if(rootNode==null)//说明 treeNode 就是根节点
         {
            rootNode=treeNode;
         }
         while( rootNode.getParentNode()!=null)
         {
             rootNode= rootNode.getParentNode();
         }
         
        if (treeNode.tag == "小组") {
            if(rootNode.name=="共享通讯录"||rootNode.name=="公司员工")
            {
                $("#btn_txlgroupdel").attr({ disabled: "disabled" });
                $("#btn_txldel").attr({ disabled: "disabled" });
                $("#btn_txlupdate").attr({ disabled: "disabled" });
                $("#btn_txladd").attr({ disabled: "disabled" });          
             }
             else
             {
                //控制进入编辑状态
                var lastselectid = $("#selectid").val();
                //如果是某个节点第二次点击，则进入编辑状态
                if (lastselectid == treeNode.id) {
                    //小组进入编辑状态
                    var treeObj = $.fn.zTree.getZTreeObj("treeDemo_txlmrg");
                    var nodes = treeObj.getNodes();
                    treeObj.editName(treeNode);
                }
             
                //按钮显隐
                $("#btn_txldel").attr({ disabled: "disabled" });
                $("#btn_txlupdate").attr({ disabled: "disabled" });
                if (treeNode.id != 0) {
                    $("#btn_txlgroupdel").removeAttr("disabled");
                    $("#btn_txladd").removeAttr("disabled");
                }
                else {
                    $("#btn_txlgroupdel").attr({ disabled: "disabled" });
                    $("#btn_txladd").attr({ disabled: "disabled" });
                }
            }
        }
        var sortype=0;
        if(rootNode.name=="公司员工")
            sortype=2;             
        else
            sortype=1;
            
        if (treeNode.tag == "组员") {
            //alert(treeNode.name);
            $.ajax({
                type: "POST",
                url: "/Txl/DoGetTxlByTxlId",
                data: { "id": treeNode.id,"type":sortype },
                success: function(txl) {
                     if(txl!=null){
                        $("#txl_name").attr("value", txl.txl_name);
                        $("#txl_cellphone").attr("value", txl.txl_cellphone);
                        $("#txl_telephone").attr("value", txl.txl_telephone);
                        $("#txl_address").attr("value", txl.txl_address);
                        $("#txl_email").attr("value", txl.txl_email);
                        $("#txl_QQ").attr("value", txl.txl_QQ);
                        $("#txl_fax").attr("value", txl.txl_fax);
                        $("#txl_dept").attr("value", txl.txl_dept);
                        $("#txl_duty").attr("value", txl.txl_duty);
                        $("#txl_descn").attr("value", txl.txl_descn);
                     }
                }
            });
                
            if(rootNode.name=="共享通讯录"||rootNode.name=="公司员工")
            {
                $("#btn_txlgroupdel").attr({ disabled: "disabled" });
                $("#btn_txldel").attr({ disabled: "disabled" });
                $("#btn_txlupdate").attr({ disabled: "disabled" });
                $("#btn_txladd").attr({ disabled: "disabled" });          
             }
             else
             {
                //按钮显隐
                $("#btn_txlgroupdel").attr({ disabled: "disabled" });
                $("#btn_txldel").removeAttr("disabled");
                $("#btn_txlupdate").removeAttr("disabled");
                $("#btn_txladd").attr({ disabled: "disabled" });
            }
        }
        
        $("#selectid").attr("value", treeNode.id);
        $("#selectname").attr("value", treeNode.name);
        $("#selecttype").attr("value", treeNode.tag);
    };

    function OnTxltreeOnRename(event, treeId, treeNode) {
        
        $.ajax({
            type: "POST",
            url: "/Txl/DoTxlGroupUpdate",
            data: { "txlgroup_id": treeNode.id, "txlgroup_name": treeNode.name },
        });
    }


    function TxlGroupAdd() {
        var group_name = $("#groupname").val();
        if (group_name == "") {
            alert("请填写小组名");
            return;
        }
        var treeObj = $.fn.zTree.getZTreeObj("treeDemo_txlmrg");
        var rootNode= treeObj.getNodeByParam("name","我的通讯录" , null);
        var node = treeObj.getNodeByParam("name",group_name , rootNode);
        if(node!=null)
        {
            alert("组名已存在");
            return;
        }
        $.ajax({
            type: "POST",
            url: "/Txl/DoTxlGroupAdd",
            data: { "group_name": group_name },
            success: function(data) {
                $("#workarea-3-5").html(data);
                intityTxlTree();
            }
        });
    }

    function TxlGroupDel() {

        var type = $("#selecttype").val();
        if (type == "组员")
            return;
        var txlgroup_id = $("#selectid").val();
        var txlgroup_name = $("#selectname").val();
        if (txlgroup_id == "") {
            alert("未选择小组");
            return;
        }
        if (txlgroup_id == 0) {
            return;
        }
        $.ajax({
            dataType: "json",
            type: "POST",
            url: "/Txl/DoTxlGroupDel",
            data: { "txlgroup_id": txlgroup_id, "txlgroup_name": txlgroup_name },
            success: function(data) {
                //                var res = eval('(' + data + ')');
                alert(data.Name);
                $("#workarea-3-5").html(res.resdata);
                intityTxlTree();
            }
        });
    }


    function TxlAdd() {
        var txlgroup_id = $("#selectid").val();
        var txlgroup_name = $("#selectname").val();
        if (txlgroup_id == "") {
            alert("未选择小组");
            return;
        }
        if (txlgroup_id == 0) {
            alert("请选择分组");
            return;
        }

        var txl_name = $("#txl_name").val();
        var treeObj = $.fn.zTree.getZTreeObj("treeDemo_txlmrg");
        var parNode= treeObj.getNodeByParam("name",txlgroup_name, null);
        var node = treeObj.getNodeByParam("name",txl_name , parNode);
        if(node!=null)
        {
            alert(txl_name+" 已存在");
            return;
        }
        
        var txl_cellphone = $("#txl_cellphone").val();
        var txl_telephone = $("#txl_telephone").val();
        var txl_address = $("#txl_address").val();
        var txl_email = $("#txl_email").val();
        var txl_QQ = $("#txl_QQ").val();
        var txl_fax = $("#txl_fax").val();
        var txl_dept = $("#txl_dept").val();
        var txl_duty = $("#txl_duty").val();
        var txl_descn = $("#txl_descn").val();
        
        if (txl_name == "") {
            alert("请填写名字");
            return;
        }
        $.ajax({
            type: "POST",
            url: "/Txl/DoTxlAdd",
            data: { "txlgroup_id": txlgroup_id, "txl_name": txl_name, "txl_cellphone": txl_cellphone, "txl_telephone": txl_telephone, "txl_address": txl_address,
                "txl_email": txl_email, "txl_QQ": txl_QQ, "txl_fax": txl_fax, "txl_dept": txl_dept, "txl_duty": txl_duty, "txl_descn": txl_descn
            },
            success: function(data) {
                $("#workarea-3-5").html(data);
                intityTxlTree();
            }
        });
    }

    function TxlDel() {
        var txl_id = $("#selectid").val();
        if (txl_id == "") {
            alert("未选择人员");
            return;
        }
        $.ajax({
            type: "POST",
            url: "/Txl/DoTxlDel",
            data: { "txl_id": txl_id },
            success: function(data) {
                $("#workarea-3-5").html(data);
                intityTxlTree();
            }
        });
    }
    
    function TxlUpdate() {
        var type = $("#selecttype").val();
        if (type == "小组"){
            alert("请选择组员");
            return;
        }
        var txl_id = $("#selectid").val();
        if (txl_id == "") {
            alert("未选择组员");
            return;
        }
        if (txl_id == 0) {
            alert("请选择组员");
            return;
        }
        var txl_name = $("#txl_name").val();
        var txl_cellphone = $("#txl_cellphone").val();
        var txl_telephone = $("#txl_telephone").val();
        var txl_address = $("#txl_address").val();
        var txl_email = $("#txl_email").val();
        var txl_QQ = $("#txl_QQ").val();
        var txl_fax = $("#txl_fax").val();
        var txl_dept = $("#txl_dept").val();
        var txl_duty = $("#txl_duty").val();
        var txl_descn = $("#txl_descn").val();
        
        $.ajax({
            type: "POST",
            url: "/Txl/DoTxlUpdate",
            data: {"txl_id":txl_id, "txl_name": txl_name, "txl_cellphone": txl_cellphone, "txl_telephone": txl_telephone, "txl_address": txl_address,
                "txl_email": txl_email, "txl_QQ": txl_QQ, "txl_fax": txl_fax, "txl_dept": txl_dept, "txl_duty": txl_duty, "txl_descn": txl_descn
            },
            success: function(data) {
                $("#workarea-3-5").html(data);
                intityTxlTree();
            }
        });
    }
</script>

<div id="div_txlmrg" style="margin-top: 5px; margin-left: 14px; width: 1090px;">
    <div id="hide">
        <input type="hidden" id="selectid" />
        <input type="hidden" id="selectname" />
        <input type="hidden" id="selecttype" />
    </div>
    <input id="btn_txlgroupadd" type="button" onclick="TxlGroupAdd()" value="添加分组" style="margin-right: 7px;" /><!--增加右边界-->
    <input id="btn_txlgroupdel" type="button" onclick="TxlGroupDel()" value="删除分组" disabled="disabled"
        style="margin-right: 7px;" /><!--增加右边界-->
    <input id="btn_txladd" type="button" onclick="TxlAdd()" value="添加组员" disabled="disabled"
        style="margin-right: 7px;" /><!--增加右边界-->
    <input id="btn_txlupdate" type="button" onclick="TxlUpdate()" value="保存修改" disabled="disabled"
        style="margin-right: 7px;" /><!--增加右边界-->
    <input id="btn_txldel" type="button" onclick="TxlDel()" value="删除组员" disabled="disabled"
        style="margin-right: 7px;" /><!--增加右边界-->
    <br />
    <div style="position: relative; top: 25px;">
        <span>分组名称：</span><input type="text" id="groupname" style="width: 224px; border: solid 1px #6C92AD;" /></div>
    <!--增加样式-->
    <div id="div_tree" class="zTreeDemoBackground left" style="border-left: solid 1px #9A9A9A;
        display: block; float: right; margin: 0; padding-left: 20px; width: 275px; height: 385px;">
        <!--增加样式-->
        <ul id="treeDemo_txlmrg" class="ztree">
        </ul>
    </div>
    <br />
    <!--分割线换行-->
    <hr style="width: 692px; float: left; margin: 30px 0;" />
    <!--增加分割线-->
    <div id="div_txladd" class="Txl_zy_infor" style="padding: 0; margin: 0;">
        <!--增加样式-->
        <span>姓名：</span>
        <input type="text" id="txl_name" style="width: 250px; border: solid 1px #6C92AD;" /><!--增加样式-->
        <span style="margin-left: 80px;">手机：</span><!--修改左边界50_80-->
        <input type="text" id="txl_cellphone" style="width: 250px; border: solid 1px #6C92AD;" /><!--增加样式-->
        <br />
        <br />
        <span style="margin-right: 2px;">Q&nbsp;Q：</span>
        <input type="text" id="txl_QQ" style="width: 250px; border: solid 1px #6C92AD;" /><!--增加样式-->
        <span style="margin-left: 53px;">固定电话：</span><!--修改左边界25_50-->
        <input type="text" id="txl_telephone" style="width: 250px; border: solid 1px #6C92AD;" /><!--增加样式-->
        <br />
        <br />
        <span>职位：</span>
        <input type="text" id="txl_duty" style="width: 250px; border: solid 1px #6C92AD;" /><!--增加样式-->
        <span style="margin-left: 80px;">邮箱：</span><!--修改左边界50_80-->
        <input type="text" id="txl_email" style="width: 250px; border: solid 1px #6C92AD;" /><!--增加样式-->
        <br />
        <br />
        <span>单位：</span>
        <input type="text" id="txl_dept" style="width: 250px; border: solid 1px #6C92AD;" /><!--增加样式-->
        <span style="margin-left: 80px;">传真：</span><!--修改左边界50_80-->
        <input type="text" id="txl_fax" style="width: 250px; border: solid 1px #6C92AD;" /><!--增加样式-->
        <br />
        <br />
        <span>地址：</span>
        <input type="text" id="txl_address" style="width: 630px; border: solid 1px #6C92AD;" /><!--增加样式-->
        <br />
        <br />
        <span style="vertical-align: top;">备注：</span>
        <textarea id="txl_descn" style="height: 90px; width: 630px; max-width: 640px; border: solid 1px #6C92AD;"></textarea><!--修改width/max-width宽度460_640-->
        <br />
    </div>
</div>
